/****************************************************************
 * Fulguro
 * Copyright (C) 2004 Christophe Clienti
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 ***************************************************************/


/****************************************************************
  This typemap allow to give the possibility to the python user to replace 
  FLGR_Vector function parametter by a scalar or a list/array.
****************************************************************/


%typecheck(SWIG_TYPECHECK_POINTER) FLGR_Vector * { 
  $1 = 1;
 }

%typemap(in) FLGR_Data1D * (FLGR_Type dattype, int datspp) {
  if ((SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor,0)) == -1) return SwigObjectEmpty;
  dattype = $1->type;
  datspp = $1->spp;
 }

%typemap(in) FLGR_Data2D * (FLGR_Type dattype, int datspp) {
  if ((SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor,0)) == -1) return SwigObjectEmpty;
  dattype = $1->type;
  datspp = $1->spp;
 }

%typemap(in) (FLGR_Vector *) {
  $1 = flgr_vector_create(datspp1, dattype1);
  
  if( ((SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor,0)) != -1)) {
    FLGR_Vector *tmp = $1;
    if($1->spp != datspp1) {
      POST_ERROR("Vector spp don't correspond to data spp\n");
      return SwigObjectEmpty;
    }
    flgr_vector_copy($1,tmp);

  }else if(SwigCheckIntType($input) || SwigCheckDblType($input)) {
    switch(dattype1) {
    case FLGR_UINT8:
      flgr_vector_populate_from_scalar_fgUINT8($1,(fgUINT8) (CUInt_FromSwigInt($input)));break;
    case FLGR_UINT16:
      flgr_vector_populate_from_scalar_fgUINT16($1,(fgUINT16) (CUInt_FromSwigInt($input)));break;
    case FLGR_UINT32:
      flgr_vector_populate_from_scalar_fgUINT32($1,(fgUINT32) (CUInt_FromSwigInt($input)));break;
    case FLGR_INT8:
      flgr_vector_populate_from_scalar_fgINT8($1,(fgINT8) (CInt_FromSwigInt($input)));break;
    case FLGR_INT16:
      flgr_vector_populate_from_scalar_fgINT16($1,(fgINT16) (CInt_FromSwigInt($input)));break;
    case FLGR_INT32:
      flgr_vector_populate_from_scalar_fgINT32($1,(fgINT32) (CInt_FromSwigInt($input)));break;
    case FLGR_FLOAT32:
      flgr_vector_populate_from_scalar_fgFLOAT32($1,(fgFLOAT32) (CDouble_FromSwigFloat($input)));break;
    case FLGR_FLOAT64:
      flgr_vector_populate_from_scalar_fgFLOAT64($1,(fgFLOAT64) (CDouble_FromSwigFloat($input)));break;
    default:
      POST_ERROR("unknown type\n");
      return SwigObjectEmpty;
    }


  }else if(SwigCheckArrayType($input)) {
    if(SwigArraySize($input) != datspp1) {
      int i;

      if($1->type==FLGR_UINT8) {
	for(i=0 ; i<datspp1 ; i++) 
	  flgr_vector_set_element_fgUINT8($1,i,(fgUINT8) (CUInt_FromSwigInt(SwigGetArrayVal($input,i))));

      }else if($1->type==FLGR_UINT16) {	
	for(i=0 ; i<datspp1 ; i++) 
	  flgr_vector_set_element_fgUINT16($1,i,(fgUINT16) (CUInt_FromSwigInt(SwigGetArrayVal($input,i))));
    
      }else if($1->type==FLGR_UINT32) {	
	for(i=0 ; i<datspp1 ; i++) 
	  flgr_vector_set_element_fgUINT32($1,i,(fgUINT32) (CUInt_FromSwigInt(SwigGetArrayVal($input,i))));
  
      }else if($1->type==FLGR_INT8) {	
	for(i=0 ; i<datspp1 ; i++) 
	  flgr_vector_set_element_fgINT8($1,i,(fgINT8) (CInt_FromSwigInt(SwigGetArrayVal($input,i))));

      }else if($1->type==FLGR_INT16) {	
	for(i=0 ; i<datspp1 ; i++) 
	  flgr_vector_set_element_fgINT16($1,i,(fgINT16) (CInt_FromSwigInt(SwigGetArrayVal($input,i))));
    
      }else if($1->type==FLGR_INT32) {	
	for(i=0 ; i<datspp1 ; i++) 
	  flgr_vector_set_element_fgINT32($1,i,(fgINT32) (CInt_FromSwigInt(SwigGetArrayVal($input,i))));
  
      }else if($1->type==FLGR_FLOAT32) {		
	for(i=0 ; i<datspp1 ; i++) 
	  flgr_vector_set_element_fgFLOAT32($1,i,(fgFLOAT32) (CDouble_FromSwigFloat(SwigGetArrayVal($input,i))));
   
      }else if($1->type==FLGR_FLOAT64) {	
	for(i=0 ; i<datspp1 ; i++) 
	  flgr_vector_set_element_fgFLOAT64($1,i,(fgFLOAT64) (CDouble_FromSwigFloat(SwigGetArrayVal($input,i))));

      }else if($1->type==FLGR_BIT) {	
	for(i=0 ; i<datspp1 ; i++) 
	  flgr_vector_set_element_fgBIT($1,i,(fgBIT) (CUInt_FromSwigInt(SwigGetArrayVal($input,i))));
   
      }else {					
	POST_ERROR("unknown type\n");
	return SwigObjectEmpty;
      }	
    }else {
      POST_ERROR("list or array specified don'correspond to data spp\n");
      return SwigObjectEmpty;
    }
  }else {
    POST_ERROR("unknown type\n");
    return SwigObjectEmpty;
  }
}

%typemap(freearg) (FLGR_Vector *) {
  flgr_vector_destroy($1);
 }

